package com.baohulu.baohulu.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;

import com.baohulu.baohulu.R;
import com.baohulu.baohulu.common.Const;
import com.baohulu.baohulu.utils.SharedPrefUtil;
import com.baohulu.baohulu.utils.ToastUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的钱包，为了方便测试，功能暂时为修改ip
 */
public class MyWalletActivity extends AppCompatActivity {
    @BindView(R.id.et_ip)
    EditText etIpAddress;
    String ipPattern = "^([0-9a-zA-Z._]+)$";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_wallet);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        String ip = SharedPrefUtil.getString(Const.SpKey.CUSTOM_IP);
        etIpAddress.setText(ip);
    }

    @OnClick(R.id.confirm)
    public void onClick() {
        String ipAddress = etIpAddress.getText().toString();
        Pattern p = Pattern.compile(ipPattern);
        Matcher matcher = p.matcher(ipAddress);
        if (matcher.matches()) {
            customIPAddress(ipAddress);
        } else {
            ToastUtil.showToast("输入 IP 不合法！");
        }
    }

    private void customIPAddress(String ipAddress) {
        SharedPrefUtil.putBoolean(Const.SpKey.IS_CUSTOM_IP, true);
        SharedPrefUtil.putString(Const.SpKey.CUSTOM_IP, ipAddress);
        ToastUtil.showToast("修改成功！");
        finish();
    }
}
